package Demo3;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

class MyThreadPool {
    private BlockingQueue<Runnable> queue = new LinkedBlockingQueue<>();
    public void submit (Runnable runnable) throws InterruptedException {
        queue.put(runnable);
    }

    public  MyThreadPool(int n) {
        for (int i = 0;i < n;i++) {
            Thread t = new Thread(()->{

                try {
                    while (true) {
                        Runnable runnable = queue.take();
                        runnable.run();
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
            t.start();
        }
    }

}
public class ThreadDemo3 {
    public static void main(String[] args) throws InterruptedException {
        MyThreadPool myThreadPool = new MyThreadPool(10);
        for (int i = 0; i < 1000; i++) {
            int num = i; // 防止变量捕获
            myThreadPool.submit(new Runnable() {
                @Override
                public void run() {
                    System.out.println("hello" + num);
                }
            });
        }
    }
}
